package wms.api.modules.user.service;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import wms.api.core.bean.ResponseErrorEntity;
import wms.api.core.cache.constants.StatusCode;
import wms.api.core.token.service.TokenService;
import wms.api.core.utils.ContextUtils;
import wms.api.modules.menu.service.MenuService;
import wms.api.modules.user.domain.LoginSuccessBean;
import wms.api.modules.user.domain.LoginUserBean;
import wms.api.modules.user.entity.UserEntity;
import wms.api.modules.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/11/13.
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuService menuService;
    @Autowired
    private TokenService tokenService;


    /**
     * 登录
     *
     * @param loginUserBean
     * @return
     */
    public ResponseEntity login(LoginUserBean loginUserBean) {
        //判断是否存在该用户
        UserEntity queryUser = new UserEntity();
        queryUser.setUserId(loginUserBean.getUserId());
        UserEntity existUser = userMapper.get(queryUser);
        //判断是否存在
        if (existUser == null) {
            return new ResponseErrorEntity(StatusCode.USER_NOT_REG, HttpStatus.BAD_REQUEST);
        }
        //密码是否匹配
        if (!loginUserBean.getPassword().equalsIgnoreCase(existUser.getPassword())) {
            return new ResponseErrorEntity(StatusCode.PASSWORD_ERROR, HttpStatus.BAD_REQUEST);
        }
        //生成token
        LoginSuccessBean successBean = new LoginSuccessBean();
        successBean.setUserId(existUser.getUserId());
        successBean.setToken(tokenService.handleToken(ContextUtils.getCurAuth().getTerminal(), existUser.getUserId()));

        return ResponseEntity.ok(successBean);
    }
}

